<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\service\ocean;

use app\enum\OceanEnum;
use app\model\OeAppExtendModel;
use app\model\OeAssetsAppModel;
use app\service\BaseService;
use think\facade\Db;

class AppExtendService extends BaseService
{
    /**
     * 获取原有app.
     * @return bool|mixed|string
     */
    public function getAppList(string $advertiserId = '')
    {
        return OeAssetsAppModel::where('advertiser_id', $advertiserId)->column('package_id');
    }

    /**
     * 获取信息.
     * @param string $advertiserId 账户id
     * @param int $accountId 账户表id
     * @param string $appId 分包id
     * @param string $channelId 渠道id
     * @param string $parentAppId 所属包id
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAppInfo(string $advertiserId = '', int $accountId = 0, string $appId = '', string $channelId = '', string $parentAppId = ''): array
    {
        $where = [
            ['channel_id', '=', $channelId],
            ['package_id', '=', $parentAppId],
            ['parent_package_id', '=', $appId],
            ['account_id', '=', $accountId],
            ['advertiser_id', '=', $advertiserId],
        ];

        $info = (new OeAppExtendModel())->where($where)->field(true)->find();
        if (! $info) {
            return [];
        }

        return $info->toArray();
    }

    /**
     * 修改信息.
     * @param int $id 主键ID
     * @param array $info 信息数据
     */
    public function updateAdvertiserData(int $id = 0, array $info = []): bool
    {
        try {
            $data = [
                'reason' => $info['reason'],
                'status' => $info['status'],
                'update_time' => $info['update_time'],
                'version_name' => $info['version_name'],
                'channel_id' => $info['channel_id'],
                'download_url' => $info['download_url'],
                'remark' => $info['remark'],
            ];
//            Db::startTrans();
            $one = (new OeAppExtendModel())->where('id', $id)->update($data);
            if ($one) {
//                Db::commit();
                return true;
            }
//            Db::rollback();
            return false;
        } catch (\Exception $e) {
            return self::setError($e->getMessage());
        }
    }

    /**
     * 插入广告报表信息.
     * @param string $advertiserId 账户ID
     * @param array $info 信息数据
     */
    public function insertAdvertiserData(string $advertiserId = '', array $info = []): bool
    {
        try {
//            Db::startTrans();
            $data = [
                'advertiser_id' => $advertiserId,
                'account_id' => $info['account_id'],
                'reason' => $info['reason'],
                'status' => $info['status'],
                'update_time' => $info['update_time'],
                'version_name' => $info['version_name'],
                'channel_id' => $info['channel_id'],
                'download_url' => $info['download_url'],
                'remark' => $info['remark'],
                'package_id' => $info['package_id'],
                'parent_package_id' => $info['parent_package_id'],
                'create_time' => time(),
            ];
            $insertId = (new OeAppExtendModel())->insertGetId($data);

            if ($insertId <= 0) {
//                Db::rollback();
                return false;
            }
//            Db::commit();
            return true;
        } catch (\Exception $e) {
            return self::setError($e->getMessage());
        }
    }

    /**
     * @return bool|mixed|string
     */
    public function getAppData(string $token = '', string $advertiserId = '', string $package_id = '', int $page = 1)
    {
        $headers = [
            'Access-Token' => $token,
            'Content-Type' => 'application/json',
        ];
        $params = [
            'advertiser_id' => $advertiserId,
            'package_id' => $package_id,
            'page' => $page,
            'page_size' => OceanEnum::MAX_PAGE_SIZE,
        ];

        return AdvertiserService::getAppExtend($headers, $params);
    }
}
